package cn.huhaoran.yaoyaolingxian.util;

import com.intellij.openapi.project.Project;
import com.intellij.terminal.pty.PtyProcessTtyConnector;
import com.jediterm.terminal.ProcessTtyConnector;
import com.jediterm.terminal.TtyConnector;
import com.jediterm.terminal.ui.TerminalPanel;
import com.jediterm.terminal.ui.settings.DefaultSettingsProvider;
import com.pty4j.PtyProcess;
import com.pty4j.PtyProcessBuilder;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class GitCommandExecutor {

    public void executeGitCommand(Project project, String gitCommand) {
        try {
            String shellCommand;
            if (isWindows()) {
                // Windows 使用 cmd.exe
                shellCommand = "cmd.exe /c " + gitCommand;
            } else {
                // macOS 和 Linux 使用 /bin/bash
                shellCommand = "/bin/bash -c " + gitCommand;
            }
            List<String> fullCommand = new ArrayList<String>();
            fullCommand.add(shellCommand);
            // 创建并启动进程
            PtyProcessBuilder processBuilder = new PtyProcessBuilder((String[]) fullCommand.toArray());
            PtyProcess process = processBuilder.start();
            TtyConnector ttyConnector = new PtyProcessTtyConnector(process, Charset.defaultCharset());

            // 创建 ProcessTtyConnector 连接到终端
        /*    TtyConnector ttyConnector = new PtyProcessTtyConnector(process, Charset.defaultCharset());
            TerminalPanel terminalPanel = new TerminalPanel(
                    new DefaultSettingsProvider(),
                    new com.jediterm.terminal.model.TerminalTextBuffer(),
                    project
            );*/
            // 设置终端面板
 /*           TerminalPanel terminalPanel = new TerminalPanel(
                new com.jediterm.terminal.model.TerminalTextBuffer(),
                new DefaultSettingsProvider(),
                project
            );*/

          /*  // 将 TtyConnector 绑定到终端
            terminalPanel.settt
            terminalPanel.setTtyConnector(ttyConnector);

            // 启动终端
            terminalPanel.start();*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().contains("win");
    }
}
